﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsField
    {
        private readonly KeRui.DAL.CmsField dal = new KeRui.DAL.CmsField();

        public int Add(KeRui.Model.CmsField model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsField> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsField> list = new List<KeRui.Model.CmsField>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsField item = new KeRui.Model.CmsField();
                    if (dt.Rows[i]["FieldID"].ToString() != "")
                    {
                        item.FieldID = int.Parse(dt.Rows[i]["FieldID"].ToString());
                    }
                    if (dt.Rows[i]["ModelID"].ToString() != "")
                    {
                        item.ModelID = int.Parse(dt.Rows[i]["ModelID"].ToString());
                    }
                    if (dt.Rows[i]["FieldType"].ToString() != "")
                    {
                        item.FieldType = int.Parse(dt.Rows[i]["FieldType"].ToString());
                    }
                    item.FieldName = dt.Rows[i]["FieldName"].ToString();
                    item.FieldAlias = dt.Rows[i]["FieldAlias"].ToString();
                    item.Tips = dt.Rows[i]["Tips"].ToString();
                    item.Description = dt.Rows[i]["Description"].ToString();
                    item.DefaultValue = dt.Rows[i]["DefaultValue"].ToString();
                    item.MaxValue = dt.Rows[i]["MaxValue"].ToString();
                    item.StyleValue = dt.Rows[i]["StyleValue"].ToString();
                    if (dt.Rows[i]["EnableNull"].ToString() != "")
                    {
                        if ((dt.Rows[i]["EnableNull"].ToString() == "1") || (dt.Rows[i]["EnableNull"].ToString().ToLower() == "true"))
                        {
                            item.EnableNull = true;
                        }
                        else
                        {
                            item.EnableNull = false;
                        }
                    }
                    if (dt.Rows[i]["EnableSearch"].ToString() != "")
                    {
                        if ((dt.Rows[i]["EnableSearch"].ToString() == "1") || (dt.Rows[i]["EnableSearch"].ToString().ToLower() == "true"))
                        {
                            item.EnableSearch = true;
                        }
                        else
                        {
                            item.EnableSearch = false;
                        }
                    }
                    if (dt.Rows[i]["IsUsing"].ToString() != "")
                    {
                        if ((dt.Rows[i]["IsUsing"].ToString() == "1") || (dt.Rows[i]["IsUsing"].ToString().ToLower() == "true"))
                        {
                            item.IsUsing = true;
                        }
                        else
                        {
                            item.IsUsing = false;
                        }
                    }
                    if (dt.Rows[i]["OrderID"].ToString() != "")
                    {
                        item.OrderID = int.Parse(dt.Rows[i]["OrderID"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    if (dt.Rows[i]["UpdateTime"].ToString() != "")
                    {
                        item.UpdateTime = DateTime.Parse(dt.Rows[i]["UpdateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int FieldID)
        {
            this.dal.Delete(FieldID);
        }

        public List<KeRui.Model.CmsField> GetList(string strWhere)
        {
            string strOrder = "OrderID ASC";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsField", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsField> GetListByCache(string strWhere)
        {
            string cacheKey = "CmsFieldList-" + strWhere;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(strWhere);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsField>) cache;
        }

        public KeRui.Model.CmsField GetModel(int FieldID)
        {
            return this.dal.GetModel(FieldID);
        }

        public KeRui.Model.CmsField GetModelByCache(int FieldID)
        {
            string cacheKey = "CmsFieldModel-" + FieldID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(FieldID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsField) cache;
        }

        public void ModifyIsUsing(int fieldID)
        {
            this.dal.ModifyIsUsing(fieldID);
        }

        public void Update(KeRui.Model.CmsField model)
        {
            this.dal.Update(model);
        }
    }
}

